package com.yanqu.road.server.cmd.activity.shuihuyezicard;

import com.yanqu.road.entity.activity.shuihuyezipai.config.ShuiHuYeZiCardRoomConfig;
import com.yanqu.road.pb.activity.ShuiHuYeZiCardProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shuihuyezicard.ShuiHuYeZiCardModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.shuihuyezicard.ShuiHuYeZiCardMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;


/**
 * @Description
 * @Author cwq
 * @Data 2021/12/9 11:33
 */
@Cmd(code = GameProtocol.S_SHUIHUYEZICARD_START_GAME_FROM_CROSS, desc = "水浒叶子牌 开始匹配 跨服返回区服 协议")
public class ShuiHuYeZiCardStartGameFromCrossCmd  extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ShuiHuYeZiCardProto.ShuiHuYeZiGetCardStartGameToServerMsg crossMsg = ShuiHuYeZiCardProto.ShuiHuYeZiGetCardStartGameToServerMsg.parseFrom(packet.getBytes());
        ShuiHuYeZiCardModule shuiHuYeZiCardModule = player.getModule(ShuiHuYeZiCardModule.class);
        if(crossMsg.getStcMsg().getRet() != 0){
            // 匹配失败
            ShuiHuYeZiCardRoomConfig roomConfig = ShuiHuYeZiCardMgr.getConfig().getRoomConfigMap().get(crossMsg.getRoomType());
            if(roomConfig != null){
                int times = shuiHuYeZiCardModule.getUserData().getJoinRoomTimesMap().getOrDefault(crossMsg.getRoomType(),0);
                if(times >= roomConfig.getFreePlay()){
                    Property consume = PropertyHelper.parseStringToProperty(roomConfig.getConsume());
                    // 解除预扣除
                    player.getModule(PreReduceModule.class).restorePreReduce(consume);
                }
            }
        }
        player.getModule(ShuiHuYeZiCardModule.class).setStatus(1);
        player.sendPacket(ClientProtocol.U_SHUIHUYEZICARD_START_GAME,crossMsg.getStcMsg().toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
